﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Simple.Services.System.Recipes.Models;

namespace Simple.WebApi.Controllers.System;

/// <summary>
/// 食材库管理
/// </summary>
[Route("api/Recipes/[action]")]
[ApiController]
[Authorize]
public class RecipesController : ControllerBase
{
    private readonly RecipesService _recipesService;
    private readonly ParentChildTasksService _parentChildTasksService;

    public RecipesController(RecipesService recipesService, ParentChildTasksService parentChildTasksService)
    {
        _recipesService = recipesService;
        _parentChildTasksService = parentChildTasksService;
    }


    /// <summary>
    /// 食谱查询
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<AppResult> Page([FromQuery] RecipesPageInputModel model)
    {
        var data = await _recipesService.GetPageAsync(model);
        return AppResult.Status200OK(data: data);
    }

    /// <summary>
    /// 食谱详情
    /// </summary>
    /// <param name="recipesId"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<AppResult> RecipesDatail([FromQuery] Guid recipesId)
    {
        var data = await _recipesService.GetRecipesDatailAsync(recipesId);
        var message = "成功";
        message = data.Count() == 0 ? "查询失败" : message;
        return AppResult.Status200OK(message,data: data);
    }

    /// <summary>
    /// 食谱增加
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<AppResult> Add([FromBody] RecipesModel model)
    {
        await _recipesService.AddAsync(model);
        return AppResult.Status200OK("新增成功");
    }

    /// <summary>
    /// 食谱编辑
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<AppResult> Edit([FromBody] RecipesModel model)
    {
        var recipesId = await _recipesService.UpdateAsync(model);
        return AppResult.Status200OK("更新成功", recipesId);
    }

    /// <summary>
    /// 食谱删除
    /// </summary>
    /// <param name="models"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<AppResult> Delete([FromBody] List<IdInputModel> models)
    {
        await _recipesService.DeleteAsync(models.Select(m => m.Id));
        return AppResult.Status200OK("删除成功");
    }



    /// <summary>
    /// 每日食谱详情(App)
    /// </summary>
    /// <param name="models"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<AppResult> DayRecipes([FromQuery] PlanRecipesInputModel models)
    {
        var data = await _recipesService.GetPlanDayRecipesAsync(models);
        return AppResult.Status200OK(data: data);
    }

    /// <summary>
    /// 增加每周计划食谱
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<AppResult> SavePlanWeek([FromBody] AddPlanRecipesDto dto)
    {
        await _recipesService.SavePlanWeek(dto);
        return AppResult.Status200OK("操作成功");
    }

    /// <summary>
    /// 每周计划食谱列表
    /// </summary>
    /// <param name="models"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<AppResult> GetPlanWeek([FromQuery] PlanWeekQueryModel models)
    {
        var data = await _recipesService.GetPlanWeekAsync(models);
        return AppResult.Status200OK(data: data);
    }

    /// <summary>
    /// 每周计划食谱详情
    /// </summary>
    /// <param name="models"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<AppResult> GetPlanWeekDetail([FromQuery] Guid PlanWeekRecipesId)
    {
        var data = await _recipesService.GetPlanWeekDetailAsync(PlanWeekRecipesId);
        return AppResult.Status200OK(data: data);
    }

    /// <summary>
    /// 计划食谱删除
    /// </summary>
    /// <param name="models"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<AppResult> DeletePlan([FromQuery] Guid planweekrecipesId)
    {
        await _recipesService.DeletePlanAsync(planweekrecipesId);
        return AppResult.Status200OK("删除成功");
    }

    /// <summary>
    /// 每日采购量
    /// </summary>
    /// <param name="models"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<AppResult> DayPurchase([FromQuery] PlanRecipesInputModel models)
    {
        var data = await _recipesService.GetPlanDayRecipesAsync(models);
        return AppResult.Status200OK(data: data);
    }

    /// <summary>
    /// 一周采购量
    /// </summary>
    /// <param name="models"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<AppResult> WeekPurchase([FromQuery] Guid PlanWeekRecipesId)
    {
        var data = await _recipesService.GetWeekPurchaseAsync(PlanWeekRecipesId);
        return AppResult.Status200OK(data: data);
    }

    /// <summary>
    /// 一周采购量
    /// </summary>
    /// <param name="models"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<AppResult> WeekPurchaseV2([FromQuery] Guid PlanWeekRecipesId)
    {
        var data = await _recipesService.GetWeekPurchaseAsyncV2(PlanWeekRecipesId);
        return AppResult.Status200OK(data: data);
    }

}
